{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e5e87132",
   "metadata": {},
   "source": [
    "## [dict](https://docs.python.org/3/tutorial/datastructures.html#dictionaries)\n",
    "\n",
    "* 基本案例\n",
    "---\n",
    ">>> tel = {'jack': 4098, 'sape': 4139}\n",
    ">>> tel['guido'] = 4127\n",
    ">>> tel\n",
    "{'jack': 4098, 'sape': 4139, 'guido': 4127}\n",
    "---\n",
    ">>> tel['jack']\n",
    "4098\n",
    "---\n",
    ">>> del tel['sape']\n",
    ">>> tel['irv'] = 4127\n",
    ">>> tel\n",
    "{'jack': 4098, 'guido': 4127, 'irv': 4127}\n",
    "---\n",
    ">>> list(tel)\n",
    "['jack', 'guido', 'irv']\n",
    "---\n",
    ">>> sorted(tel)\n",
    "['guido', 'irv', 'jack']\n",
    ">>> 'guido' in tel\n",
    "True\n",
    ">>> 'jack' not in tel\n",
    "False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d25ae65",
   "metadata": {},
   "source": [
    "# 字典的查询\n",
    "\n",
    "> 1.in方法查询\n",
    "> 2.get方法获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7e323688",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}\n"
     ]
    }
   ],
   "source": [
    "#Zhichao\n",
    "\n",
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3e8620a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询key 是否在字典当中 用in\n",
    "\"stu1801\" in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "60819c0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get方法获取字典中的值（key所对应的value的内容）\n",
    "info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "27c5ab07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ZiQi\n"
     ]
    }
   ],
   "source": [
    "# 综上所述，字典的查询方法一般步骤如下：\n",
    "if \"stu1801\" in info:\n",
    "    print(info.get(\"stu1801\"))\n",
    "else:\n",
    "    print(\"您查询的内容不存在\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d560698b",
   "metadata": {},
   "source": [
    "# 字典的删除\n",
    "\n",
    "> 1. pop()——指定值删除\n",
    "> 2. popitem()——随机删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0d9cc35d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c53ef913",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "pop expected at least 1 argument, got 0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-d4c4e3f3113c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0minfo\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: pop expected at least 1 argument, got 0"
     ]
    }
   ],
   "source": [
    "info.pop()\n",
    "# pop()里要有值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8153e6df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.pop('stu1801')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0337ab40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2e790a6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1803', 'LiuYu')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.popitem()\n",
    "# 随机删除——用元组形式展示"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbf905ce",
   "metadata": {},
   "source": [
    "## 字典的一般方法\n",
    "\n",
    "> 1. 字典的循环遍历\n",
    "> 2. fromkeys()\n",
    "> 3. setdefalut()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1248aa09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "99bdc3d2",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "too many values to unpack (expected 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-25-9508ebb76568>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 1. 字典的循环遍历\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# 错误示范\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mv\u001b[0m \u001b[1;32min\u001b[0m \u001b[0minfo\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: too many values to unpack (expected 2)"
     ]
    }
   ],
   "source": [
    "# 1. 字典的循环遍历\n",
    "# 错误示范\n",
    "for k,v in info:\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0b6d51b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu1801 ZiQi\n",
      "stu1802 XiaoFeng\n",
      "stu1803 LiuYu\n"
     ]
    }
   ],
   "source": [
    "# 1. 字典的循环遍历\n",
    "# 循环遍历的最大用途：给所有的值使用同样的操作/方法\n",
    "\n",
    "for k,v in info.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4952558b",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'info' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-25f8d1520706>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 取出学号，不要stu，取在学号的list里\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mid_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mv\u001b[0m \u001b[1;32min\u001b[0m \u001b[0minfo\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'info' is not defined"
     ]
    }
   ],
   "source": [
    "# 取出学号，不要stu，取在学号的list里\n",
    "id_list = []\n",
    "for k,v in info.items():\n",
    "    print(k[3:],v)\n",
    "    id_list.append(k[3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "673018df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1801', '1802', '1803']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "64933657",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-6-abfdc983888f>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-6-abfdc983888f>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    for k,v in info\u001b[0m\n\u001b[1;37m                   ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 练习：把info的value目前的格式进行统一修改，修改的范式为：姓 名 Zi Qi\n",
    "last_name = []\n",
    "for k,v in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "339003b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ZiQi\n",
      "4\n",
      "XiaoFeng\n",
      "8\n",
      "LiuYu\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "last_name = []\n",
    "for k,v in info.items():\n",
    "    print(v)\n",
    "    print(len(v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f90dcac",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "57d84982",
   "metadata": {},
   "source": [
    "### fromkeys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "28cf0368",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "296d9662",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': None, 'age': None, 'sexual': None}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu1801_detail={}\n",
    "stu1801_detail.fromkeys([\"name\",\"age\",\"sexual\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "871bc34d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'气血': 10, '力量': 10, '魔法值': 10}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化字典信息\n",
    "游戏人物属性={}\n",
    "游戏人物属性.fromkeys([\"气血\",\"力量\",\"魔法值\"],10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "57eb0453",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu2101': 123456, 'stu2102': 123456, 'stu2103': 123456}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "学生初始密码={}\n",
    "学生初始密码.fromkeys([\"stu2101\",\"stu2102\",\"stu2103\"],123456)\n",
    "# 初始化统一值的信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5582c289",
   "metadata": {},
   "source": [
    "### setdefalut()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e02e1d0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7843a540",
   "metadata": {},
   "outputs": [],
   "source": [
    "info.setdefault('stu1804')\n",
    "# 如果该值存在，查询该值，如果不存在，则加进去\n",
    "# 不给值则默认为None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "600f8acd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu', 'stu1804': None}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "16fe4728",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhichao'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.setdefault('stu1805','zhichao')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c5d6d9ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi',\n",
       " 'stu1802': 'XiaoFeng',\n",
       " 'stu1803': 'LiuYu',\n",
       " 'stu1804': None,\n",
       " 'stu1805': 'zhichao'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e66291a",
   "metadata": {},
   "source": [
    "### 课本练习（P109） "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "700e7c2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n",
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "found = {}\n",
    "## 左列是元音，右列是频度\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "# found\n",
    "\n",
    "## 赋值\n",
    "found['e'] = found['e']+1\n",
    "# found\n",
    "\n",
    "## 快捷方式\n",
    "found['e'] += 1\n",
    "# found\n",
    "\n",
    "## 迭代处理 key 和 value\n",
    "# for k in found:\n",
    "#     print(k,'was found',found[k],'time(s).')\n",
    "\n",
    "## sorted函数（内置函数，控制输出顺序）\n",
    "for k in sorted(found):\n",
    "    print(k,'was found',found[k],'time(s).')\n",
    "    \n",
    "## 内置——运用 items \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc2195ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
